package com.seed.uaa.util;

import com.seed.core.query.pojo.Declared;

import java.util.LinkedHashMap;
import java.util.Map;

public class DeclaredUtil {

    public static Map<String, Declared> merge(Map<String, Declared> declareds, Map<String, Declared> others) {
        boolean isDeclaredsEmpty = declareds == null || declareds.isEmpty();
        boolean isOthersEmpty = others == null || others.isEmpty();

        if(isDeclaredsEmpty && isOthersEmpty) return new LinkedHashMap<>();

        if(isDeclaredsEmpty) return others;
        if(isOthersEmpty) return declareds;

        for(Map.Entry<String, Declared> entry: others.entrySet()) {
            String key = entry.getKey();
            Declared declared = declareds.get(key);
            Declared other = entry.getValue();
            if(declared == null) {
                declareds.put(key, other);
                continue;
            }

            if(!declared.getType().equals(other.getType())) {
                throw new RuntimeException("类型不匹配");
            }

            merge(declared, other);
        }

        return declareds;
    }
}
